package P150;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiangMu
 * @version \$Id: P36.java, v 0.1, 2024年07月06日 00:18 LiangMu Exp $$
 */
public class P36 {

    public static boolean canConstruct(String ransomNote, String magazine) {
        if (magazine.length() < ransomNote.length()) {
            return false;
        }
        Map<Character, Integer> countMap = new HashMap<>();
        for (char c : magazine.toCharArray()) {
            int i = countMap.getOrDefault(c, 0) + 1;
            countMap.put(c, i);
        }

        char[] chars = ransomNote.toCharArray();
        for (char c : chars) {
            if (countMap.containsKey(c) && countMap.get(c) > 0) {
                countMap.put(c, countMap.get(c) - 1);
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(canConstruct("aa", "aab"));
    }

}
